package cn.com.lms.business.livecourse.persistence.repository;

import cn.com.lms.business.livecourse.persistence.entity.CourseLiveSign;
import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * CourseLiveSignRepository
 *
 * @author barry
 * @date 2020/3/30
 **/
@Repository
public interface CourseLiveSignRepository extends BaseRepository<CourseLiveSign, Long> {

    // 记录直播课程签到记录的签出时间
    @Modifying
    @Query(value = "UPDATE bnz_course_live_sign SET duration=duration+?2,exit_time=?3 WHERE id=?1", nativeQuery = true)
    int updateSignInfo(long id, int duration, Date exitTime);

    // 查询对应学习计划阶段课程的已有签到信息
    @Query(value = "select * from bnz_course_live_sign where stage_detail_id=?1 and account=?2", nativeQuery = true)
    List<CourseLiveSign> existSigns(long stageDetailId, String account);

    // 查询对应学生当天总学习时间
    @Query(value = "select sum(duration) as todayTotalDuration from bnz_course_live_sign where account=?1 and enter_time>=?2 and exit_time<?3", nativeQuery = true)
    Map todayTotalDuration(String account, Date startDate, Date endDate);

    // 查询课程学时
    @Query(value = "select course_time from bnz_course_info where id=?1", nativeQuery = true)
    int courseTime(long id);

    // 获取系统配置信息
    @Query(value = "select value from bnz_sys_setting where _key=?1 limit 1", nativeQuery = true)
    String sysSettingValue(String key);
}
